import os

from lagent.agents.rewoo import ReWOO
from langchain.agents import AgentType
from langchain.agents import initialize_agent
from langchain.agents import load_tools
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain_openai import OpenAI

os.environ["SERPAPI_API_KEY"] = "eedd2489d2f50dc4f302d914908069b6e17500e42f809c8ea0b45f17186498e0"

"""
    ReAct示例：AgentExecutor
    pip install langchain
"""
llm = OpenAI(temperature=0.9,
             base_url="https://api.openai-hk.com/v1",
                     api_key='hk-0amgwp10000255022bdd816341db25b54dc2e46787aee69f')
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.invoke("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")


"""
    plan and execute示例：PlanAndExecute
    pip install -U langchain langchain_experimental
"""
planner = load_chat_planner(llm)
executor = load_agent_executor(llm, tools, verbose=True)
# 初始化Plan-and-Execute Agent
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
# 运行Agent解决新问题（完善了需求）
agent.invoke("查查玫瑰的库存然后给出50朵玫瑰的价格和当天的配送方案！")


"""
   ReWOO示例：ReWOO 
   pip install lagent
"""
rewoo = ReWOO(
    model_name_or_path="gpt-3.5-turbo",  # 指定LLM模型
    openai_api_key="YOUR_OPENAI_API_KEY"  # 替换为你的OpenAI API 密钥
)
# 向LLM发送提示并获取回答
prompt = "What is the capital of France?"
response = rewoo.chat(prompt)
# 打印回答
print(response.choices[0].text)


"""
    LLMCompiler示例：ParallelAgentRunner
    pip install llama-index-agent-llm-compiler
"""
from llama_index.core.agent import ParallelAgentRunner
from llama_index.agent.llm_compiler.step import LLMCompilerAgentWorker

agent_worker = LLMCompilerAgentWorker.from_tools(
    tools, llm=llm, verbose=True)
agent = ParallelAgentRunner(agent_worker)
# start using the agent
response = agent.chat("What is (121 * 3) + 42?")
